package liwei;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import java.util.Arrays;
import java.util.logging.Level;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;

/**
 * a Frame to send out an email using Mail class also written by myself
 * we can get the information we want from this frame
 * @author LiWei
 *
 */
public class EmailFrame extends JFrame
{
	private JPanel contentPane;
	private JTextField txtExampleemailcom;
	private JTextField txtExampleemailcom_1;
	private JTextField txtSmtpexamplecom;
	private JTextField textField;
	private JTextField textField_2;
	private String textString;
	private JPasswordField passwordField;
	private JTextField textField_1;
	/**
	 * get the text to be sent from the parameter
	 */
	public void setText(String str)
	{
		textString = str;
	}
	/**
	 * the getter method of textString
	 * @return the textString
	 */
	public String getText()
	{
		return textString;
	}

	/**
	 * the constructor method of the frame
	 */
	public EmailFrame()
	{
		setTitle("send an email");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 364, 305);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		//some of the sentences are created automatically
		txtExampleemailcom = new JTextField();
		txtExampleemailcom.setBounds(147, 10, 153, 21);
		txtExampleemailcom.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				txtExampleemailcom.setText("");
			}
		});
		contentPane.setLayout(null);
		txtExampleemailcom.setText("example@email.com");
		contentPane.add(txtExampleemailcom);
		txtExampleemailcom.setColumns(10);
		
		JLabel lblSendFrom = new JLabel("Send From");
		lblSendFrom.setBounds(10, 13, 73, 15);
		contentPane.add(lblSendFrom);
		
		txtExampleemailcom_1 = new JTextField();
		txtExampleemailcom_1.setBounds(147, 41, 153, 21);
		txtExampleemailcom_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				txtExampleemailcom_1.setText("");
			}
		});
		txtExampleemailcom_1.setText("example@email.com");
		contentPane.add(txtExampleemailcom_1);
		txtExampleemailcom_1.setColumns(10);
		
		JLabel lblSendTo = new JLabel("Send To");
		lblSendTo.setBounds(10, 44, 73, 15);
		contentPane.add(lblSendTo);
		
		JButton btnSend = new JButton("Send");
		btnSend.setBounds(150, 234, 93, 23);
		final Mail aMail = new Mail();
		aMail.setEmailFrame(this);
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try
				{
					aMail.init();
					aMail.send();
				} catch (Exception e)
				{
					// TODO 自动生成的 catch 块
					//e.printStackTrace();
					Main.logger.log(Level.ALL,"Can't send this email");
				}
			}
		});
		contentPane.add(btnSend);
		
		txtSmtpexamplecom = new JTextField();
		txtSmtpexamplecom.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				txtSmtpexamplecom.setText("");
			}
		});
		txtSmtpexamplecom.setText("smtp.example.com");
		txtSmtpexamplecom.setBounds(147, 72, 153, 21);
		contentPane.add(txtSmtpexamplecom);
		txtSmtpexamplecom.setColumns(10);
		
		JLabel lblSmpHost = new JLabel("Smtp host");
		lblSmpHost.setBounds(10, 75, 73, 15);
		contentPane.add(lblSmpHost);
		
		textField = new JTextField();
		textField.setBounds(147, 103, 153, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel lblUserName = new JLabel("user name");
		lblUserName.setBounds(10, 109, 73, 15);
		contentPane.add(lblUserName);
		
		JLabel lblPassword = new JLabel("password");
		lblPassword.setBounds(10, 140, 73, 15);
		contentPane.add(lblPassword);
		
		textField_2 = new JTextField();
		textField_2.setBounds(147, 169, 153, 21);
		contentPane.add(textField_2);
		textField_2.setColumns(10);
		
		JLabel lblSubject = new JLabel("subject");
		lblSubject.setBounds(10, 171, 54, 15);
		contentPane.add(lblSubject);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(147, 137, 153, 21);
		contentPane.add(passwordField);
		
		textField_1 = new JTextField();
		textField_1.setBounds(147, 203, 153, 21);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblSignature = new JLabel("signature");
		lblSignature.setBounds(10, 206, 54, 15);
		contentPane.add(lblSignature);
	}
	public String getSendFrom()
	{
		return txtExampleemailcom.getText();
	}
	public String getSendTo()
	{
		return txtExampleemailcom_1.getText();
	}
	public String getSmtp()
	{
		return txtSmtpexamplecom.getText();
	}
	public String getUserName()
	{
		return textField.getText();
	}
	public String getPassword() 
	{
		return Arrays.toString(passwordField.getPassword());
	}
	public String getSubject()
	{
		return textField_2.getText();
	}
	public String getSignature()
	{
		return textField_1.getText();
	}
}
